package com.lm.minio.utils;

import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * @ClassName: MinioUtil
 * @Description: MINIO 操作类
 * @Author:    shdxhl
 * @Date:       16:05 2023-07-13
 * @Email:  wlsk131420@gmail.com
 * @Version: 1.0.
 */
@Component
public class MinioUtil {

    @Autowired
    private MinioClient client;

    /**
     * 创建桶
     * @Author shdxhl
     * @Method  createBucket
     * @Date 9:13 2023-06-13
     * @param bucketName 桶名称
     * @return void
     **/
    @SneakyThrows
    public void createBucket(String bucketName) {
        boolean found =
                client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!found) {
            client.makeBucket(
                    MakeBucketArgs.builder()
                            .bucket(bucketName)
                            .region("cn-beijing")
                            .build());
        }
    }

    /**
     * 删除桶
     * @Author shdxhl
     * @Method  removeBucket
     * @Date 9:13 2023-06-13
     * @param bucketName 桶名称
     * @return void
     **/
    @SneakyThrows
    public void removeBucket(String bucketName) {
        client.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 获取桶文件信息
     * @Author shdxhl
     * @Method  getObjectInfo
     * @Date 9:13 2023-06-13
     * @param bucketName 桶名称
     * @param objectName 文件名称
     * @return io.minio.ObjectStat
     **/
    @SneakyThrows
    public ObjectStat getObjectInfo(String bucketName, String objectName) {
        return client.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    /**
     * 上传文件
     * @Author shdxhl
     * @Method  putObject
     * @Date 9:13 2023-06-13
     * @param bucketName 桶名称
     * @param objectName 文件名
     * @param stream     流
     * @param fileSize   文件大小
     * @param type       文件类型
     * @return void
     * @throws Exception
     **/
    public void putObject(String bucketName, String objectName, InputStream stream, Long fileSize, String type) throws Exception {
        client.putObject(
                PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
                                stream, fileSize, -1)
                        .contentType(type)
                        .build());
    }

    /**
     * 判断文件夹是否存在
     * @Author shdxhl
     * @Method  folderExists
     * @Date 9:13 2023-06-13
     * @param bucketName 桶名称
     * @param prefix     文件夹名字
     * @return Boolean
     **/
    @SneakyThrows
    public Boolean folderExists(String bucketName, String prefix) {
        Iterable<Result<Item>> results = client.listObjects(ListObjectsArgs.builder().bucket(bucketName)
                .prefix(prefix).recursive(false).build());
        for (Result<Item> result:results){
            Item item = result.get();
            if (item.isDir()){
                return true;
            }
        }
        return false;
    }

    /**
     * 创建文件夹
     * @Author shdxhl
     * @Method  createFolder
     * @Date 9:13 2023-06-13
     * @param bucketName 桶名称
     * @param path 路径
     * @return void
     **/
    @SneakyThrows
    public void createFolder(String bucketName,String path) {
        client.putObject(PutObjectArgs.builder().bucket(bucketName).object(path)
                .stream(new ByteArrayInputStream(new byte[]{}),0,-1).build());
    }

    /**
     * 获取文件在minio在服务器上的外链
     * @Author shdxhl
     * @Method  createFolder
     * @Date 9:13 2023-06-13
     * @param bucketName 桶名称
     * @param objectName 文件名
     * @return String
     **/
    @SneakyThrows
    public String getObjectUrl(String bucketName,String objectName){
        return client.getPresignedObjectUrl(
                GetPresignedObjectUrlArgs.builder()
                        .method(Method.GET)
                        .bucket(bucketName)
                        .object(objectName)
                        .build());
    }

}
